<template>
    <ContentWrap>
        <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px" label-width="70px" @submit.native.prevent>
            <el-form-item label="归属学校" prop="schoolId" v-if="tenantId == 1">
                <el-select v-model="queryParams.schoolId" placeholder="请选择" class="!w-240px" filterable default-first-option clearable>
                    <el-option v-for="item in schools" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
            </el-form-item>
            <el-form-item label="专业名称" prop="name">
                <el-input v-model="queryParams.name" class="!w-240px" clearable placeholder="请输入专业名称" @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item>
                <el-button @click="handleQuery">
                    <Icon class="mr-5px" icon="ep:search"/>
                    搜索
                </el-button>
                <el-button @click="resetQuery">
                    <Icon class="mr-5px" icon="ep:refresh"/>
                    重置
                </el-button>
                <el-button v-hasPermi="['yxt:major:create']" plain type="primary" @click="openForm('create')">
                    <Icon class="mr-5px" icon="ep:plus"/>
                    新增
                </el-button>
            </el-form-item>
        </el-form>
    </ContentWrap>

    <ContentWrap>
        <el-table v-loading="loading" :data="list">
            <el-table-column label="学校" prop="schoolName" min-width="150"/>
            <el-table-column label="学生数量" prop="stuCount" width="100"/>
            <el-table-column label="老师数量" prop="teaCount" width="100"/>
            <el-table-column label="专业名称" prop="name" min-width="150"/>
            <el-table-column label="备注" prop="remark" min-width="250"/>
            <el-table-column :formatter="dateFormatter" align="center" label="创建时间" prop="createTime" width="180"/>
            <el-table-column :width="150" align="center" label="操作" fixed="right">
                <template #default="scope">
                    <el-button v-hasPermi="['yxt:major:update']" link type="primary" @click="openForm('update', scope.row.id)">
                        编辑
                    </el-button>
                    <el-button v-hasPermi="['yxt:major:delete']" link type="danger" @click="handleDelete(scope.row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total" @pagination="getList"/>
    </ContentWrap>

    <MajorForm ref="formRef" @success="getList"/>
</template>

<script lang="ts" setup>
import {dateFormatter} from '@/utils/formatTime'
import {useMajorApi} from "@/api/yxt/major";
import MajorForm from "@/views/yxt/major/MajorForm.vue";
import {useConfirm, useMessage} from "@/components/Notice";
import {useSchoolApi} from "@/api/yxt/school";
import {getTenantId} from "@/utils/auth";

defineOptions({name: 'YxtMajor'})

const tenantId = getTenantId();

const {t} = useI18n()
const majorApi = useMajorApi();
const schoolApi = useSchoolApi();

const loading = ref(true)
const total = ref(0)
const list = ref([])
const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    schoolId: '',
    code: '',
    name: '',
    status: undefined,
    createTime: []
})
const queryFormRef = ref()
const schools = ref<Array<any>>([])

const getList = async () => {
    loading.value = true
    try {
        const data = await majorApi.getMajorPage(queryParams)
        list.value = data.list
        total.value = data.total
    } finally {
        loading.value = false
    }
}

const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
}

const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id)
}

const handleDelete = (id: number) => {
    useConfirm(t("common.delMessage"), async () => {
        await majorApi.deleteMajor(id);
        getList().finally(() => useMessage(t('common.delSuccess')));
    });
}

onMounted(async () => {
    getList();
    schools.value = await schoolApi.getSimpleList();
})
</script>
